package com.hnnu.shoppingtoghter.demo.controller;

import com.github.pagehelper.PageInfo;
import com.hnnu.shoppingtoghter.demo.entity.Message;
import com.hnnu.shoppingtoghter.demo.entity.Project;
import com.hnnu.shoppingtoghter.demo.entity.SysUser;
import com.hnnu.shoppingtoghter.demo.service.MessageService;
import com.hnnu.shoppingtoghter.demo.service.ProjectService;
import com.hnnu.shoppingtoghter.demo.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description:
 * @Author: liu_cheng_xi
 * @CreateTime： 2022/7/1 11:59
 */

@Controller
@Slf4j
public class ProjectController {

    @Autowired
    private ProjectService projectService;
    @Autowired
    private MessageService messageService;


    /**
     * @Description: 在展示页面展示出所有的商品
     * @Author: liu_cheng_xi
     * @CreateTime: 2022/7/4 11:25
     * @param: project
     * @param: model
     * @return: java.lang.String
     **/

    @GetMapping("/mall_list")
    public String index(Project project, Model model) {
        model.addAttribute("project", project);
        System.out.println("project =" + project);
        List<Project> projectList = projectService.selectListByProject(project);
        System.out.println("projectList =" + projectList);
        model.addAttribute("projectList", projectList);

        /**  */
        PageInfo<Project> pageInfo = new PageInfo<>(projectList);
        //控制台输出看一下
        System.out.println("pageInfo=" + pageInfo);
        model.addAttribute("pageInfo", pageInfo);
        return "mall_list";
    }


    /**
     * @Description: 初始话个人商品页面
     * @Author: liu_cheng_xi
     * @CreateTime: 2022/7/5 14:58
     * @param: null
     * @return: null
     **/

    @GetMapping("/my_note")
    public String UserProject(Model model, Project project,HttpSession session){
            /** 获取当前登录的用户*/
            SysUser loginuser = (SysUser)session.getAttribute("user");
            System.out.println("当前登录的人是："+loginuser.getUserName());
            project.setCommodityUser(loginuser.getId());


            model.addAttribute("project1", project);
            System.out.println("project1 =" + project);
            List<Project> projectList1 = projectService.selectProjectByUser(project);
            System.out.println("projectList1 =" + projectList1);
            model.addAttribute("projectList1", projectList1);

            /** 构造分页对象 */
            PageInfo<Project> pageInfo = new PageInfo<>(projectList1);
            //控制台输出看一下
            System.out.println("pageInfo=" + pageInfo);
            model.addAttribute("pageInfo", pageInfo);
        return "my_note";
    }


    /**
     * @Description: 初始化添加页面
     * @Author: liu_cheng_xi
     * @CreateTime: 2022/7/5 9:13
     * @return: java.lang.String
     **/

    @GetMapping("/write_note")
    public String saveProject(Model model) {
        Project project = new Project();
        model.addAttribute("project", project);
        return "write_note";
    }

    /**
     * @Description: 将用户提交的商品信息插入到对应的表中
     * @Author: liu_cheng_xi
     * @CreateTime: 2022/7/4 11:25
     * @param: project
     * @param: model
     * @return: java.lang.String
     **/

    @PostMapping("/write_note")
    public String add(Project project,HttpSession session) {
        System.out.println("接收到的商品信息：" + project);
        /** 获取当前登录的用户*/
        SysUser loginuser = (SysUser)session.getAttribute("user");
        project.setCommodityUser(loginuser.getId());
        int row = projectService.insertProject(project);
        if (row == 0) {
            System.out.println("插入失败");
        }
        return "redirect:/mall_list";
    }

/**
 * @Description: 初始化修改页面
 * @Author: liu_cheng_xi
 * @CreateTime: 2022/7/5 11:12
 * @param: model
 * @param: commodityId
 * @return: java.lang.String
 **/

@GetMapping("/my_note2/{commodityId}")
    public String UpdateProject(Model model,@PathVariable("commodityId") Integer commodityId){
    Project project = projectService.selectProjectById(commodityId);
    model.addAttribute("project",project);
        return "my_note2";
}

/**
 * @Description: 修改商品信息
 * @Author: liu_cheng_xi
 * @CreateTime: 2022/7/5 11:22
 * @param: null
 * @return: null
 **/
@PutMapping("/my_note2")
    public String Update(Project project){
    System.out.println("接受到需要修改的商品是："+ project);
    int rows = projectService.Update(project);
    if(rows == 0){
        System.out.println("用户更新失败");
        return "my_note2";
    }
    return "redirect:/my_note";
}

/**
 * @Description: 实现商品的删除
 * @Author: liu_cheng_xi
 * @CreateTime: 2022/7/6 15:25
 * @param: null
 * @return: null
 **/
@DeleteMapping("/my_note/{commodityId}")
    public String DeleteProject(@PathVariable("commodityId") Integer commodityId){
    log.info("想要删除的商品：" + commodityId);
    int rows = projectService.DeleteProject(commodityId);
    if (rows > 0){
        return "success";
    }
    return "error";
}

/**
 * @Description: 通过ID获取商品的详情信息
 * @Author: liu_cheng_xi
 * @CreateTime: 2022/7/7 14:59
 * @param: null
 * @return: null
 **/
@GetMapping("/project/{commodityId}")
    public String ProductDetails(@PathVariable("commodityId") Integer commodityId, Model model, Message message){
    Project project = projectService.selectProjectById(commodityId);
    model.addAttribute("project",project);


    message.setMessageCommodity(commodityId);
    model.addAttribute("message", message);
    System.out.println("message =" + message);
    List<Message> messageList = messageService.selectListByMessage(message);
    System.out.println("messageList =" + messageList);
    model.addAttribute("messageList", messageList);

    /**  */
    PageInfo<Message> pageInfo = new PageInfo<>(messageList);
    //控制台输出看一下
    System.out.println("pageInfo=" + pageInfo);
    model.addAttribute("pageInfo", pageInfo);
    return "project";
}

/**
 * @Description: 添加留言信息
 * @Author: liu_cheng_xi
 * @CreateTime: 2022/7/8 13:53
 * @param: null
 * @return: null
 **/

@PostMapping("/project")
    public String addMessage(Message message,HttpSession session,Project project,Model model){
    model.addAttribute("project1", project);
    System.out.println("project1 =" + project);
    List<Project> projectList1 = projectService.selectProjectByUser(project);
    System.out.println("projectList1 =" + projectList1);
    model.addAttribute("projectList1", projectList1);



    System.out.println("拿到的留言信息："+message);
    SysUser loginuser = (SysUser)session.getAttribute("user");
    message.setMessageUser(loginuser.getId());
    message.setMessageCommodity(project.getCommodityId());
    int row = messageService.insertMessage(message);
    if (row == 0) {
        System.out.println("插入失败");
    }

    model.addAttribute("message", message);
    System.out.println("message =" + message);
    List<Message> messageList = messageService.selectListByMessage(message);
    System.out.println("messageList =" + messageList);
    model.addAttribute("messageList", messageList);

    /**  */
    PageInfo<Message> pageInfo = new PageInfo<>(messageList);
    //控制台输出看一下
    System.out.println("pageInfo=" + pageInfo);
    model.addAttribute("pageInfo", pageInfo);


    return "project";
}


}
